package net.zjitc.controller.admin;

import net.zjitc.common.ServiceResp;
import net.zjitc.mapper.OrderMapper;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Order;
import net.zjitc.pojo.OrderItem;
import net.zjitc.pojo.Products;
import net.zjitc.service.CategoryService;
import net.zjitc.service.OrderService;
import net.zjitc.service.ProductService;
import net.zjitc.vo.ProductVo;
import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author admin
 * @Date 2019/4/3 10:36
 */
@Controller
@RequestMapping("/admin/product")
public class ProductsController {
    @Autowired
    ProductService productService;
    @Autowired
    CategoryService categoryService;


    @RequestMapping(value = "/list")
    public String All_list(Model model){
        List<Products>list = productService.findAll();
        List<Category>categoryList = categoryService.findAll();
//        for (Products p :list
//             ) {
//            Category category = categoryService.findById(p.getCid());
//            p.setCategory(category);
//        }
        model.addAttribute("list",list);
        model.addAttribute("clist",categoryList);
        return "/admin/products/list.jsp";
    }

    @RequestMapping(value = "/addui")
    public String addui(Model model){
        List<Category> categoryList = categoryService.findAll();
        model.addAttribute("clist",categoryList);
        return "/admin/products/add.jsp";

    }

    @RequestMapping(value = "/addProduct")
    @ResponseBody
    public ServiceResp addProduct(@RequestParam(value = "file",required = false)MultipartFile file, HttpServletRequest request, Products products){
        if (file != null)
        {
            String path = request.getServletContext().getRealPath("/productss/6");
            boolean flag = processFile(products,file,path);
            if (flag != true){
                return ServiceResp.createByERROR("文件上传失败");
            }
        }
        int count = productService.add(products);
        if (count>0)
        {
            return ServiceResp.createBySuccess("商品添加成功",0);
        }
        return ServiceResp.createByERROR("商品添加失败",1);
    }

    @RequestMapping(value = "/deleteProduct")
    public String deleteProduct(String id){
        productService.delete(id);
        return "redirect:/admin/product/list";
    }

    @RequestMapping(value = "/findProductById")
    public String findProductById(String  id,Model model){
        Products products = productService.findById(id);
        List<Category>categoryList = categoryService.findAll();
        model.addAttribute("clist",categoryList);
        model.addAttribute("p",products);
        return "/admin/products/edit.jsp";
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public ServiceResp update(Products products,@RequestParam(value = "file",required = false) MultipartFile file, HttpServletRequest request){
        if (file != null)
        {
            String path = request.getServletContext().getRealPath("/productss/6");
            if (!processFile(products,file,path))
            {
                return ServiceResp.createByERROR("商品修改失败");
            }
        }
        int update = productService.update(products);
        if (update>0)
        {
            return ServiceResp.createBySuccess("商品修改成功",0);
        }
        return ServiceResp.createByERROR("商品修改失败",1);
    }



    @RequestMapping("/search")
    @ResponseBody
    public List<Products> search(ProductVo vo){
        List<Products> list = productService.search(vo);
        return list;
    }



    private boolean processFile(Products products,MultipartFile file,String path){
        if (file != null)
        {
            File dir = new File(path);
            if ((!dir.exists())||(!dir.isDirectory()))
            {
                dir.mkdirs();
            }
            String filename = UUID.randomUUID().toString()+"-"+file.getOriginalFilename();
            File filepath = new File(dir, filename);
            try {
                file.transferTo(filepath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            products.setImgurl("/productss/6/"+filename);
        }
        return true;
    }





}
